/**
 * Created by Administrator on 2017/7/6 0006.
 */



var getBossindexContent = function(){
    console.log("获取信息");
    $.ajax({
        url:'/controller/getindexContent',
        type:'post',
        dataType:'json',
        data:{},
        success:function(data){
            if(data.code != 200){
                return ;
            }else{
                for(var i in data.info){
                    console.log("ddddddddddddddddddddd")
                    console.log(data.info[i]);
                    $("#bossIndexInfo").html(data.info[i].content);
                }
            }
        }
    });
}

var getBossPCindexContent = function(){
    console.log("获取信息");
    $.ajax({
        url:'/controller/getPcindexContent',
        type:'post',
        dataType:'json',
        data:{},
        success:function(data){
            if(data.code != 200){
                return ;
            }else{
                for(var i in data.info){
                    console.log("ddddddddddddddddddddd")
                    console.log(data.info[i]);
                    $("#bossIndexInfo").html(data.info[i].content);
                }
            }
        }
    });
}





var getBossAboutAsInfo = function(){
    console.log("获取信息");
    $.ajax({
        url:'/controller/getAboutAsInfo',
        type:'post',
        dataType:'json',
        data:{},
        success:function(data){
            if(data.code != 200){
                return ;
            }else{
                var html = "";
                for(var i in data.info){
                    console.log(data.info[i]);
                    if(data.info[i].id == 0){
                        $("#info0").html(data.info[i].content)
                    }else if(data.info[i].id == -1){
                        $("#info-1").html(data.info[i].content)
                    }else {
                        html += '<tr><th><textarea id="info'+ data.info[i].id +'" class="col-lg-10" style="width: 100%;height: 90px"> '+ data.info[i].content
                            +'</textarea></th><th>'
                            +'<a onclick="changeAboutUsMsg({id:'+  data.info[i].id +'})">提交</a>'
                            +'</th></tr>'
                    };
                }
                console.log("html is:",html);
                var tbody = $('#theWay tbody');
                var tr = $(html);
                tbody.append(tr);

            }
        }
    });
}



var getBossCompneyImag = function(){
    console.log("获取信息");
    $.ajax({
        url:'/controller/getBossCompneyImag',
        type:'post',
        dataType:'json',
        data:{},
        success:function(data){
            if(data.code != 200){
                return ;
            }else{
                var html = "";


                for(var i in data.info){
                    var id = JSON.stringify({url:data.info[i].id,id:data.info[i].url,name:'compneyImageContent'});

                    html += '<tr>'
                        +  '<th>'
                        + '<img src="'+ data.info[i].url + '" style="width: 40%">'
                        +'</th>'
                        +'<th>'
                        // + '<form  enctype="multipart/form-data">'
                        + ' <input id="companyImg'+ data.info[i].id +'" name="" type="file" class="form-control"/>'

                        +  '<button class="btn btn-primary col-lg-12" onclick="changeImags('
                        + "{"
                        + "id:"+data.info[i].id
                        + ",url:'" + data.info[i].url +"'"
                        + ",fileName:'" + "companyImg" +"'"
                        +"}"
                        + ')">提交</button>'
                        //  + '</form>'
                        +'</th>'
                        +'</tr>'
                }
            }
            console.log("html is:",html);
            var tbody = $('#compneyImageContent tbody');
            var tr = $(html);
            tbody.append(tr);

        }

    });
}

var  changeImags =function (info) {

    console.log("id is:",info);

    if(info.id != 0)
        var divid ="#" + info.fileName + info.id;
    else
        var divid = "#" + info.fileName

    console.log("divid:",divid);

    if(!$(divid)[0].files[0])
        return alert("清选择需要上传的文件");

    var Nmysql = 0;
    if(info.Nmysql)
        Nmysql = info.Nmysql;

    var formData = new FormData();
    formData.append('files',$(divid)[0].files[0]);
    formData.append('id',info.id);
    formData.append('url',info.url);
    formData.append('Nmysql',Nmysql);

    console.log("formData",formData)

    $.ajax({
        url: '/controller/upload',
        type: 'POST',
        cache: false,
        data: formData,
        processData: false,
        contentType: false
    }).success(function(data) {
        console.log(data);
        if(200 === data.code) {
            alert("成功");
            location.reload();
        } else {
            alert("失败");
        }

    }).error(function () {
        alert("与服务器通信发生错误");
    })




    /*
     debugger;
     $.ajax({
     url: '/controller/upload',
     type: 'POST',
     data: formData,
     async: false,
     cache: false,
     contentType: false,
     processData: true,
     success: function(data){
     console.log(data);
     debugger;
     if(200 === data.code) {
     alert("成功");
     } else {
     alert("失败");
     }
     console.log('imgUploader upload success, data:', data);
     },
     error: function(){
     alert("与服务器通信发生错误");
     }
     });*/
}




var getBossNewsInfo = function(){
    console.log("获取信息");
    $.ajax({
        url:'/controller/getNewsInfo',
        type:'post',
        dataType:'json',
        data:{},
        success:function(data){
            if(data.code != 200){
                return ;
            }else{
                var html = "";
                for(var i in data.info){
                    html += '<tr>' +
                        '<th><textarea id="title'+ data.info[i].topId +'" class="col-lg-10" style="width: 100%;height: 90px"> '+ data.info[i].title +'</textarea></th>' +
                        '<th><textarea id="time'+ data.info[i].topId +'" class="col-lg-10" style="height: 90px"> '+ data.info[i].time +'</textarea></th>' +
                      //  '<th><textarea id="topContent'+ data.info[i].topId +'" class="col-lg-10" style="width: 100%;height: 90px"> '+ data.info[i].topContent +'</textarea></th>' +
                        '<th><textarea id="detailContent'+ data.info[i].topId +'" class="col-lg-10" style="width: 100%;height: 90px"> '+ data.info[i].detailContent +'</textarea></th>' +
                        '<th><a onclick="changeNewsInfo('+  data.info[i].topId +')">提交</a></th></tr>'
                }
            }
            console.log("html is:",html);
            var tbody = $('#compneyImageContent tbody');
            var tr = $(html);
            tbody.append(tr);

        }

    });
}

var addImgInfo = function () {
    var id = $("#compneyImageContent tr").length + 1;

    console.log("id is",id);

    var html = "";
    html += '<tr>'
        +  '<th>'
        + '<img  style="width: 40%">'
        +'</th>'
        +'<th>'
        // + '<form  enctype="multipart/form-data">'
        + ' <input id="companyImg'+ id +'" name="" type="file" class="form-control"/>'

        +  '<button class="btn btn-primary col-lg-12" onclick="changeImags('
        + "{"
        + "id:"+ id
        + ",url:'" + "/image/compneyImageContent0"  +  id +"'"
        +"}"
        + ')">提交</button>'
        //  + '</form>'
        +'</th>'
        +'</tr>'

    var tbody = $('#compneyImageContent tbody');
    var tr = $(html);
    tbody.append(tr);
}


var addNewInfo= function () {
    var id = $("#compneyImageContent tr").length + 1;

    console.log("id is",id);

    var html = "";
    html += '<tr>' +
        '<th><textarea id="title'+ id +'" class="col-lg-10" style="width: 100%;height: 90px"> '+ ""  +'</textarea></th>' +
        '<th><textarea id="time'+ id +'" class="col-lg-10" style="height: 90px"> '+ "" +'</textarea></th>' +
        // '<th><textarea id="topContent'+ id +'" class="col-lg-10" style="width: 100%;height: 90px"> '+ "" +'</textarea></th>' +
        '<th><textarea id="detailContent'+ id +'" class="col-lg-10" style="width: 100%;height: 90px"> '+ "" +'</textarea></th>' +
        '<th><a onclick="changeNewsInfo('+  id +')">提交</a></th></tr>'

    var tbody = $('#compneyImageContent tbody');
    var tr = $(html);
    tbody.append(tr);
}

var changeNewsInfo = function (id) {
    var obj = {};
    obj.title = $("#title"+ id).val();
    obj.time = $("#time"+ id).val();
    obj.topContent = $("#topContent"+ id).val();
    obj.detailContent = $("#detailContent"+ id).val();
    obj.topId =id;

    $.ajax({
        url:'/controller/changeNewsInfo',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data){
            alert(data.info);
            if(data.code == 200)
                location.reload();
        }
    });
};



var getbossContactInfo = function(){
    console.log("获取信息");
    $.ajax({
        url:'/controller/getContactInfo',
        type:'post',
        dataType:'json',
        data:{},
        success:function(data){
            if(data.code != 200){
                return ;
            }else{
                var html = "";
                for(var i in data.info){
                    html += '<tr>' +
                        '<th><input id="title'+ data.info[i].id +'" class="col-lg-10"  value="'+ data.info[i].title +'"> </input></th>' +
                        '<th><input id="content'+ data.info[i].id +'" class="col-lg-10" value="'+ data.info[i].content +'"> </input></th>' +
                        '<th><a onclick="changeContactInfo('+  data.info[i].id +')">提交</a></th></tr>'
                }
            }
            console.log("html is:",html);
            var tbody = $('#compneyImageContent tbody');
            var tr = $(html);
            tbody.append(tr);
        }
    });
}


var changeContactInfo = function (id) {
    var obj = {};
    obj.title = $("#title"+ id).val();
    obj.content = $("#content"+ id).val();
    obj.id =id;

    $.ajax({
        url:'/controller/ChangeContactInfo',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data){
            alert(data.info)
        }
    });
};


//修改首页文字描述
var changeIndexMsg = function (info) {
    var obj = {};
    obj.msg = $("#"+info.idName).val();

    $.ajax({
        url:'/controller/changeIndexMsg',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data){
            alert(data.info);
            if(data.code == 200)
                location.reload();
        }
    });
};


//修改首页文字描述
var changeAboutUsMsg = function (info) {
    var obj = {};

    obj.id = info.id;
    obj.msg = $("#info"+info.id).val();

    $.ajax({
        url:'/controller/changeAboutUsMsg',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data){
            alert(data.info);
            if(data.code == 200)
                location.reload();
        }
    });
};

